//
//  SignViewController.m
//  parent
//
//  Created by yanzhen on 15/7/17.
//  Copyright (c) 2015年 ci123. All rights reserved.
//

#import "SignViewController.h"
#import "RuleViewController.h"
#import "AFAppClient.h"
#import "PopUIManager.h"
#import "Config.h"
#import "UIImageView+WebCache.h"

@interface SignViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
@property (weak, nonatomic) IBOutlet UIView *containerview;
@property NSMutableArray *circles;
@property (weak, nonatomic) IBOutlet UIImageView *photo;
@property (weak, nonatomic) IBOutlet UILabel *nickname;
@property (weak, nonatomic) IBOutlet UILabel *gold;
@property (weak, nonatomic) IBOutlet UILabel *signNo;
@property NSMutableArray *labls;
@property UILabel *current_label;
@end

@implementation SignViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title=@"签到";
    [self CustomBackButton];
    _circles = [NSMutableArray new];
    _labls = [NSMutableArray new];
    [self initCircle];
    [self getSignLog];
    
    NSDictionary *info = [[Config Instance]getUInfo];
    _nickname.text = [info objectForKey:@"nickname"];
    _gold.text = [NSString stringWithFormat:@"%@莱斯币",[info objectForKey:@"gold"]];
    [_photo sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[info objectForKey:@"pic"]]] placeholderImage:[UIImage imageNamed:@"default_avatar"]];
}
-(void)getSignLog
{
    [self showHud:@"正在签到" isDim:YES];
    NSDictionary *params = @{@"uid":@([[Config Instance]getUID])};
    [[AFAppClient sharedManager]getWithURL:[NSString stringWithFormat:@"%@%@",SERVER_URL_NEW,kUserSign] withParameters:params success:^(NSDictionary *responseObject) {
        if([[responseObject objectForKey:@"result"]integerValue]==2){
            [self showHudComplete:@"签到成功" success:YES];
        }else if([[responseObject objectForKey:@"result"]integerValue]==1){
            [self showHudComplete:@"今日已经签过到" success:NO];
        }
        [[AFAppClient sharedManager]getWithURL:[NSString stringWithFormat:@"%@%@",SERVER_URL_NEW,kUserSignLog] withParameters:params success:^(NSDictionary* responseObject) {
            NSDictionary *result = [responseObject objectForKey:@"result"];
            NSUInteger dayNum = [[result objectForKey:@"dayNum"]integerValue];
            UIImageView *curr_image;
            for(NSUInteger i=0;i<7;i++){
                if(dayNum>i){
                    UIImageView *imageView =  [_circles objectAtIndex:i];
                    [imageView setImage:[UIImage imageNamed:@"黄圈"]];
                    if(dayNum == i+1){
                        curr_image = imageView;
                    }
                }
                UILabel *tmpLabel = [_labls objectAtIndex:i];
                tmpLabel.text =[NSString stringWithFormat:@"+%@", [result objectForKey:[NSString stringWithFormat:@"day_%ld",(i+1)]]];
                tmpLabel.textAlignment=NSTextAlignmentCenter;
            }
            [_current_label setFrame:CGRectMake(curr_image.frame.origin.x, 88, 30, 21)];
            [_current_label setText:[NSString stringWithFormat:@"%ld天",dayNum]];
            [_signNo setText:[NSString stringWithFormat:@"已签到%ld天",dayNum]];
        } failure:^(NSError *error) {
            
        }];
    } failure:^(NSError *error) {
        [self showHudComplete:@"网络连接失败" success:NO];
    }];
}

-(void)initCircle
{
    for(NSUInteger i=0;i<7;i++){
        UIImage *image;
        if(i==0){
            image = [UIImage imageNamed:@"黄圈"];
        }else{
            image = [UIImage imageNamed:@"白圆圈"];
        }
        UIImageView *circle = [[UIImageView alloc]initWithImage:image];
        [self.containerview addSubview:circle];
        [_circles addObject:circle];
        NSUInteger total_width = APP_SCREEN_WIDTH-40;
        NSUInteger padding = (total_width- 210)/6 + 1.2;
        [circle setFrame:CGRectMake(16 + (30+padding)*i, 61, 30, 30)];
        UILabel *lbl = [UILabel new];
        [_labls addObject:lbl];
        [lbl setText:@"+100"];
        [lbl setTextColor:UIColorMake(225, 185, 93)];
        [lbl setFont:[UIFont systemFontOfSize:10.0]];
        [lbl setFrame:CGRectMake(17+(30+padding)*i, 61, 30,30)];
        [self.containerview addSubview:lbl];
    }
    _current_label = [UILabel new];
    [_containerview addSubview:_current_label];
    [_current_label setTextColor:UIColorMake(226, 193, 121)];
    [_current_label setText:@"1天"];
    [_current_label setFont:[UIFont systemFontOfSize:10]];
    [_current_label setFrame:CGRectMake(16, 88, 30, 21)];
    [_current_label setTextAlignment:NSTextAlignmentCenter];
}
-(void)viewDidAppear:(BOOL)animated
{
    self.scrollview.contentSize = CGSizeMake(APP_SCREEN_WIDTH, 660);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)rule:(id)sender {
    RuleViewController *rule = [[RuleViewController alloc]init];
    rule.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:rule animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
